package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl  implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条

        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    //分页查询
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //1、构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2、创建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(pagesize)
                .skip((page - 1) * pagesize);
        //3、调用mongoTemplate查询
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4、构建返回值PageResult
        return  new PageResult(page,pagesize,count,list);
    }

    @Override
    public RecommendUser queryRecommendUser(Long userId, Long id) {
        Query query=Query.query(Criteria.where("toUserId").is(userId))
                .addCriteria(Criteria.where("userId").is(id));
        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
        if(one == null) {
            one = new RecommendUser();
            one.setUserId(id);
            one.setToUserId(userId);
            //构建缘分值
            one.setScore(95d);
        }
        return one;
    }
    //查询推荐卡片
    @Override
    public List<RecommendUser> queryRecommendUsers(Long userId, int count) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Criteria criteria=Criteria.where("toUserId").is(userId).and("userId").nin(likeUserId);
        TypedAggregation<RecommendUser> aggregation=Aggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),Aggregation.sample(count));
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        return results.getMappedResults();
    }

}
